//
//  DrawView.m
//  30206
//
//  Created by student on 16/4/7.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "DrawView.h"
@interface DrawView()
{
    CGFloat spiderY[50];
    CADisplayLink *link;
}
@property(nonatomic,strong) UIImage *spider;
@end
@implementation DrawView
-(void)setPlaying:(BOOL)playing
{
    _playing=playing;
    link.paused=!playing;
}
-(UIImage *)spider
{
    if(_spider==nil)
    {
        _spider=[UIImage imageNamed:@"spider"];
    }
    return _spider;
}
- (void)drawRect:(CGRect)rect
{
    int count=rect.size.width/self.spider.size.width;
    for(int i=0;i<count;i++)
    {
        spiderY[i]+=arc4random_uniform(5);
        CGFloat x=i*self.spider.size.width;
        CGFloat y=spiderY[i];
        [self.spider drawAtPoint:CGPointMake(x, y)];
        if(spiderY[i]>=rect.size.height)
        {
            spiderY[i]=0;
        }
    }
}
-(void)awakeFromNib
{
    if(link==nil)
    {
        link=[CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
        [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
        link.paused=YES;
    }
}

@end
